package at.fhooe.mcm.deadreckoning.host.gui;

import java.awt.EventQueue;
import java.util.StringTokenizer;

/**
 * @class DeadReckoningInfoGUI
 * @author Florian Lettner, Lukas Bischof, Peter Riedl
 * @version 1.0
 *
 * @brief The GUI for viewing all information provided by the DeadReckoning system.
 *
 * Displays filter coefficients, accelerations, computed velocities and positions
 * as well as the distance and the averaged distance of two sensors.
 */
public class DeadReckoningInfoGUI extends javax.swing.JFrame {

    /** 
     * @brief Creates new form DeadReckoningInfoGUI.
     */
    public DeadReckoningInfoGUI() {
        initComponents();
        setVisible(true);
    }

    /**
     * @brief This method is called from within the constructor to
     * initialize the form.
     *
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        accelerationXField = new javax.swing.JLabel();
        accelerationYField = new javax.swing.JLabel();
        positionXField = new javax.swing.JLabel();
        positionYField = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        velocityXField = new javax.swing.JLabel();
        velocityYField = new javax.swing.JLabel();
        distanceField = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        intervalField = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        distanceAvgField = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        inputXField = new javax.swing.JLabel();
        inputYField = new javax.swing.JLabel();
        correctedXField = new javax.swing.JLabel();
        correctedYField = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DeadReckoningDataWatcher");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Euler Method", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Acceleration X:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Acceleration Y:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Position X:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Position Y:");

        accelerationXField.setFont(new java.awt.Font("Tahoma", 0, 14));
        accelerationXField.setForeground(new java.awt.Color(255, 0, 0));
        accelerationXField.setText("----------");

        accelerationYField.setFont(new java.awt.Font("Tahoma", 0, 14));
        accelerationYField.setForeground(new java.awt.Color(255, 0, 0));
        accelerationYField.setText("----------");

        positionXField.setFont(new java.awt.Font("Tahoma", 0, 14));
        positionXField.setForeground(new java.awt.Color(255, 0, 0));
        positionXField.setText("----------");

        positionYField.setFont(new java.awt.Font("Tahoma", 0, 14));
        positionYField.setForeground(new java.awt.Color(255, 0, 0));
        positionYField.setText("----------");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel9.setText("Velocity X:");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel10.setText("Velocity Y:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel11.setForeground(new java.awt.Color(0, 153, 0));
        jLabel11.setText("Distance:");

        velocityXField.setFont(new java.awt.Font("Tahoma", 0, 14));
        velocityXField.setForeground(new java.awt.Color(255, 0, 0));
        velocityXField.setText("----------");

        velocityYField.setFont(new java.awt.Font("Tahoma", 0, 14));
        velocityYField.setForeground(new java.awt.Color(255, 0, 0));
        velocityYField.setText("----------");

        distanceField.setFont(new java.awt.Font("Tahoma", 0, 24));
        distanceField.setForeground(new java.awt.Color(0, 153, 0));
        distanceField.setText("--------");

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel15.setForeground(new java.awt.Color(0, 153, 0));
        jLabel15.setText("m");

        jLabel16.setText("d/dt");

        intervalField.setText("----------");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(0, 153, 0));
        jLabel12.setText("Distance Avg:");

        distanceAvgField.setFont(new java.awt.Font("Tahoma", 0, 24));
        distanceAvgField.setForeground(new java.awt.Color(0, 153, 0));
        distanceAvgField.setText("--------");

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel17.setForeground(new java.awt.Color(0, 153, 0));
        jLabel17.setText("m");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(positionYField)
                            .addComponent(accelerationXField)
                            .addComponent(accelerationYField))
                        .addGap(99, 99, 99)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel9))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(velocityXField)
                                    .addComponent(velocityYField)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel11))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(distanceAvgField)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel17))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(distanceField)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel15))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel16)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(intervalField))))))
                    .addComponent(positionXField))
                .addContainerGap(98, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(positionXField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(positionYField))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(accelerationXField))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(accelerationYField)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(velocityXField)
                                    .addComponent(jLabel9))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(velocityYField)
                                    .addComponent(jLabel10))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel11)
                                    .addComponent(distanceField)
                                    .addComponent(jLabel15))))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(distanceAvgField)
                    .addComponent(jLabel17))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(intervalField)
                    .addComponent(jLabel16))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Kalman Filter", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel19.setText("Input X:");

        jLabel22.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel22.setText("Input Y:");

        jLabel25.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel25.setText("Corrected X:");

        jLabel28.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel28.setText("Corrected Y:");

        inputXField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        inputXField.setForeground(new java.awt.Color(255, 0, 0));
        inputXField.setText("----------");

        inputYField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        inputYField.setForeground(new java.awt.Color(255, 0, 0));
        inputYField.setText("----------");

        correctedXField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        correctedXField.setForeground(new java.awt.Color(0, 153, 0));
        correctedXField.setText("----------");

        correctedYField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        correctedYField.setForeground(new java.awt.Color(0, 153, 0));
        correctedYField.setText("----------");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel25)
                    .addComponent(jLabel19))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputXField)
                    .addComponent(correctedXField))
                .addGap(107, 107, 107)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel28)
                    .addComponent(jLabel22))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(inputYField)
                    .addComponent(correctedYField))
                .addGap(237, 237, 237))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(inputXField)
                    .addComponent(jLabel22)
                    .addComponent(inputYField))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel25)
                    .addComponent(correctedXField)
                    .addComponent(jLabel28)
                    .addComponent(correctedYField))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel accelerationXField;
    private javax.swing.JLabel accelerationYField;
    private javax.swing.JLabel correctedXField;
    private javax.swing.JLabel correctedYField;
    private javax.swing.JLabel distanceAvgField;
    private javax.swing.JLabel distanceField;
    private javax.swing.JLabel inputXField;
    private javax.swing.JLabel inputYField;
    private javax.swing.JLabel intervalField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel positionXField;
    private javax.swing.JLabel positionYField;
    private javax.swing.JLabel velocityXField;
    private javax.swing.JLabel velocityYField;
    // End of variables declaration//GEN-END:variables

    /**
     * @brief Sets the passed data thread safe for the several GUI fields.
     *
     * @param _data a "|" separated list of data.
     */
    public void setData(String _data) {
        final StringTokenizer st = new StringTokenizer(_data, "|");
        if (st.countTokens() != 13) {
            System.out.println("invalid data passed in gui");
            return;
        }

        EventQueue.invokeLater(new Runnable() {

            public void run() {
                accelerationXField.setText(delimit(st.nextToken(), 3));
                accelerationYField.setText(delimit(st.nextToken(), 3));
                velocityXField.setText(delimit(st.nextToken(), 3));
                velocityYField.setText(delimit(st.nextToken(), 3));
                positionXField.setText(delimit(st.nextToken(), 3));
                positionYField.setText(delimit(st.nextToken(), 3));
                distanceField.setText(delimit(st.nextToken(), 3));
                intervalField.setText(delimit(st.nextToken(), 3));
                inputXField.setText(delimit(st.nextToken(), 3));
                inputYField.setText(delimit(st.nextToken(), 3));
                correctedXField.setText(delimit(st.nextToken(), 3));
                correctedYField.setText(delimit(st.nextToken(), 3));
                distanceAvgField.setText(delimit(st.nextToken(), 3));
            }
        });
    }

    /**
     * @brief Delimits the passed string representation of a floating point value
     * to the specified digits after the period.
     *
     * @param _str The string to delimit.
     * @param _digAfterPeriod The number of digits after the period.
     * @return The delimited string.
     */
    private String delimit(String _str, int _digAfterPeriod) {
        if (_str.indexOf(".") == -1) {
            return _str;
        }
        if (_str.substring(_str.indexOf(".") + 1).length() < _digAfterPeriod) {
            return _str;
        }
        return _str.substring(0, _str.indexOf(".") + _digAfterPeriod + 1);
    }
}
